/*
 * File Name : Unit_Test.cpp
 * Auther : dream.liuguobiao
 * Create Date : 2010-5-6
 * Last Edit : 2010-5-15
 */

#define BOOST_TEST_MODULE NODE_TEST
#include <boost/test/included/unit_test.hpp>
#include <iostream>
#include <vector>

#include "node.h"
#include "node_factory.h"
#include "file.h"
#include "directory.h"
#include "file_system.h"

#include "user.h"
#include "users.h"
#include "group.h"
#include "groups.h"

#include "command_factory.h"
#include "command.h"
#include "ls.h"

#include "shell_controller.h"
#include "shell_environment.h"

#include "my_string.h"

//test node ,file , directory and so on class=====================================================================
BOOST_AUTO_TEST_SUITE( NODE_TEST )

// Class directory Unite Test
BOOST_AUTO_TEST_CASE( directory_test )
{
node *my_directory = new directory();

std::string name("unnamed");
BOOST_CHECK( name == *my_directory->get_name() );

BOOST_CHECK( 0600 == my_directory->get_mod() );
BOOST_CHECK( NULL == my_directory->get_brother() );
BOOST_CHECK( NULL == my_directory->get_father() );
BOOST_CHECK( 4 == my_directory->get_size() );

name = "haha";
my_directory->set_name( &name );
BOOST_CHECK( name == *my_directory->get_name() );
BOOST_CHECK( 0 == my_directory->get_type() );

my_directory->set_mod( 0777 );
BOOST_CHECK( 0777 == my_directory->get_mod() );

BOOST_CHECK( NULL == my_directory->get_son() );
my_directory->set_son( my_directory );
BOOST_REQUIRE_EQUAL( my_directory , my_directory->get_son() );

}

// Class file Unite Test
BOOST_AUTO_TEST_CASE ( file_test )
{
node *my_file = new file();
std::string name("unnamed");
BOOST_CHECK( name == *my_file->get_name() );

BOOST_CHECK( 0600 == my_file->get_mod() );
BOOST_CHECK( NULL == my_file->get_brother() );
BOOST_CHECK( NULL == my_file->get_father() );

name = "my_file";
my_file->set_name( &name );
BOOST_CHECK( name == *my_file->get_name() );
BOOST_CHECK( 1 == my_file->get_type() );

my_file->set_mod( 0777 );
BOOST_CHECK( 0777 == my_file->get_mod() );

name = "";
BOOST_CHECK( name == *my_file->get_data() );
my_file->set_data( &name );
BOOST_CHECK( name == *my_file->get_data() );

BOOST_REQUIRE_EQUAL( 0 , my_file->get_size() );
}

// Class file_system Unite Test
BOOST_AUTO_TEST_CASE ( file_system_test )
{
file_system *my_file_system =  file_system::get_file_system();
node_factory *my_node_factory = node_factory::get_node_factory();
node *insert_node;
std::string name( "super_root" );
std::string type( "directory" );

BOOST_CHECK( NULL != my_file_system );
BOOST_REQUIRE_EQUAL( my_file_system->get_root() , my_file_system->get_current_directory() );
BOOST_REQUIRE_EQUAL( name ,*my_file_system->get_root()->get_name() );

std::vector<node*> *find_result ;
name = "boot" ;
find_result = my_file_system->find_node( &name );
BOOST_CHECK( name == *(*find_result)[0]->get_name() );

name = "new_node" ;
insert_node = my_node_factory->get_node( &type );
BOOST_CHECK( (std::string)"unnamed" == *(insert_node->get_name() ) );
insert_node->set_name( &name );
my_file_system->insert_node( insert_node ) ;

find_result = my_file_system->find_node( &name );

// for (unsigned i=0; i<find_result->size(); i++)
//   std::cout<<(* ((*find_result)[i]->get_name()) )<<std::endl;

 BOOST_REQUIRE_EQUAL( *( (*find_result)[0]->get_name() ) , *(insert_node->get_name() ) );


my_file_system->set_current_directory( insert_node );
BOOST_CHECK( insert_node == my_file_system->get_current_directory() );

insert_node = insert_node->get_father() ;
my_file_system->set_current_directory( insert_node );
//std::cout<<*(insert_node->get_name())<<std::endl<<*(my_file_system->get_current_directory()->get_name() )<<std::endl;
BOOST_CHECK( insert_node == my_file_system->get_current_directory() );

my_file_system->delete_node( &name );
BOOST_REQUIRE_EQUAL( 0 , my_file_system->find_node( &name )->size() );
}

BOOST_AUTO_TEST_SUITE_END ()


//test user and group class=================================================================================
BOOST_AUTO_TEST_SUITE( USER_GROUP_TEST )

// Class user and users Unite Test
BOOST_AUTO_TEST_CASE( user_test )
{
std::string name("dream");
users *user_list = users::get_users();
user *dream = new user( name );
BOOST_CHECK( name == *dream->get_name() );
//BOOST_CHECK( 1000 == dream->get_GID() );

user_list->insert_user( dream );
BOOST_REQUIRE_EQUAL( 1 , user_list->get_user_count() );
BOOST_CHECK( 500 == dream->get_ID() );
dream = NULL;
dream = user_list->find_user_by_name( &name );

BOOST_CHECK( name == *(dream->get_name()) );

user_list->delete_user_by_name( &name );
BOOST_REQUIRE_EQUAL( 0 , user_list->get_user_count() );
}


BOOST_AUTO_TEST_CASE( group_test )
{
std::string name( "users" );
groups *group_list = groups::get_groups();
group *users = new group( name );
BOOST_CHECK( name == *(users->get_name() ) );

group_list->insert_group( users );
BOOST_REQUIRE_EQUAL( 1 ,group_list->get_group_count() );
BOOST_CHECK( 100 == users->get_ID() );
users = NULL ;
users = group_list->find_group_by_name( &name );

BOOST_CHECK (name == *( users->get_name() ) ) ;

group_list->delete_group_by_name( &name ) ;
BOOST_REQUIRE_EQUAL( 0 , group_list->get_group_count() );
}


BOOST_AUTO_TEST_SUITE_END ()

BOOST_AUTO_TEST_SUITE( MY_STRING_TEST )

BOOST_AUTO_TEST_CASE( my_string_test )
{
  my_string *MyString = my_string::get_my_string();
  std::string new_string("ls -l /");
  std::string *sub_string;
 sub_string = MyString->get_sub_string( new_string );
  BOOST_CHECK ( "ls" == *sub_string );
  BOOST_CHECK ( "-l /" == new_string );

  sub_string = MyString->get_sub_string( new_string );
  BOOST_CHECK ( "-l" == *sub_string );
  BOOST_CHECK ( "/" == new_string );

}

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE( MAIN_TEST )

BOOST_AUTO_TEST_CASE( main_test )
{
  shell_controller *my_linux = new shell_controller();
  my_linux->run();


}

BOOST_AUTO_TEST_SUITE_END()

